package lucid.hrms.management.language;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.binding.message.MessageBuilder;
import org.springframework.stereotype.Component;
import org.springframework.webflow.action.MultiAction;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;

@Component("languageAction")
public class LanguageAction extends MultiAction {
	private static final Logger logger = LoggerFactory.getLogger(LanguageAction.class);

	@Autowired
	private LanguageService languageService;

	public Event saveLanguage(RequestContext context) {
		logger.debug("saveLanguage");
		try {
			Language language = (Language) context.getViewScope().get("language");
			language = languageService.saveLanguage(language);
			context.getViewScope().put("language", language);
			context.getMessageContext().addMessage(new MessageBuilder().info().defaultText("Language has been saved successfully").build());

			return success();
		} catch (Exception e) {
			logger.debug("Exception: " + e);
			context.getMessageContext().addMessage(
					new MessageBuilder().error().defaultText("Failed to save Language record. Reason: " + e.getMessage()).build());
			return error();
		}
	}
}
